/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.tabbedproperties.composite;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.commands.factories.GenericCommandFactory;
import com.cea.papyrus.core.editor.MultiDiagramEditor;
import com.cea.papyrus.profile.ui.composites.ISectionComposite;


/**
 * 
 */
public class NameComposite implements ISectionComposite {

	/**
	 * 
	 */
	private Text nameText;

	/**
	 * 
	 */
	private Composite parent;
	
	/**
	 * 
	 */
	private Element element;

	/**
	 * 
	 */
	private String sectionLabel;

	/**
	 * 
	 */
	protected DirectEditFocusListener directEditFocusListener = new DirectEditFocusListener();
	
	/**
	 * 
	 */
	protected KeyAdapter keyAdapter = new EditKeyAdapter();


	/**
	 * Creates a new NameComposite.
	 * 
	 * @param sectionName the name of the section
	 */
	public NameComposite(String sectionName){
		super();
		sectionLabel = sectionName;
	}

	/**
	 * 
	 * 
	 * @return the element
	 */
	public Element getElement() {
		return element;
	}

	/**
	 * 
	 * 
	 * @param element the element to set
	 */
	public void setElement(Element element) {
		this.element = element;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public String getText() {
		return nameText.getText();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#createContent(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory)
	 */
	/**
	 * 
	 * 
	 * @param factory 
	 * @param parent 
	 * 
	 * @return 
	 */
	public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
		Composite composite = factory.createFlatFormComposite(parent);
		this.parent = parent;
		FormData data;

		nameText = factory.createText(composite, ""); //$NON-NLS-1$
		nameText.setEditable(true);
		
		// nameText.addFocusListener(directEditFocusListener);
		data = new FormData();
		data.width = 150;
		data.left = new FormAttachment(0, AbstractPropertySection.STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(100, -ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(0, 0);
		nameText.setLayoutData(data);

		CLabel nameLabel = factory.createCLabel(composite, sectionLabel); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(nameText,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(nameText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);
		return composite;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#refresh()
	 */
	/**
	 * 
	 */
	public void refresh() {
		if(!nameText.isDisposed()) {
			if(element instanceof NamedElement) {
				nameText.removeFocusListener(directEditFocusListener);
				nameText.removeKeyListener(keyAdapter);
				String name = ((NamedElement)element).getName();
				if(name==null) {
					nameText.setText("");
				} else {
					nameText.setText(name); 
				}
					
				
				nameText.setEditable(true);
			}

			if(element instanceof ElementImport) {
				nameText.removeFocusListener(directEditFocusListener);
				nameText.removeKeyListener(keyAdapter);
				String name = ((ElementImport)element).getName();
				if(name==null) {
					nameText.setText("");
				} else {
					nameText.setText(name); 
				}
				nameText.setEditable(false);
			}
			nameText.addFocusListener(directEditFocusListener);
			nameText.addKeyListener(keyAdapter);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		if(nameText!=null && !nameText.isDisposed()){
			nameText.removeFocusListener(directEditFocusListener);
			nameText.removeKeyListener(keyAdapter);
		}
	}

	/**
	 * sets the behavioral feature.
	 * 
	 * @param element the behavioral feature that owns the displayed parameters
	 */
	public void setInput(Element element) {
		setElement(element);
	}

	/**
	 * 
	 */
	private class EditKeyAdapter extends KeyAdapter {

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		@Override
		public void keyPressed(KeyEvent e) {
			// close the text editor and copy the data over 
			// when the user hits "ENTER"
			if (e.character == SWT.CR) {

				// get text in nameText and save it into the model
				if(element instanceof ElementImport){// in the case of element import
					// do nothing
				} else if(element instanceof NamedElement) {
					Command command = GenericCommandFactory.eINSTANCE.createCommand(element, "Name", new Object[] {nameText.getText().trim()});
					// Darpan
					IEditorPart ie = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
					CommandStack stack = null;
					stack = (CommandStack) ie.getAdapter(CommandStack.class);
					stack.execute(command);
					ie.setFocus();
					// ((NamedElement)element).setName(nameText.getText().trim());
				}

			}
			// close the text editor when the user hits "ESC"
			if (e.character == SWT.ESC) {
				if(element instanceof ElementImport){// in the case of element import
					// do nothing
				} else if(element instanceof NamedElement) {
					// export text
					nameText.setText(((NamedElement)element).getName());
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().setFocus();
				}
			}
		}
	}


	/**
	 * Listener for a direct edit manager.
	 * 
	 * @author Remi SCHNEKENBURGER
	 */
	protected class DirectEditFocusListener implements org.eclipse.swt.events.FocusListener  {
		// check if the name has been changed or not
		/**
		 * 
		 */
		String label = "";
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusGained(FocusEvent e){
			label = getText().trim();
		}

		/**
		 * 
		 * 
		 * @param e 
		 */
		public void focusLost(FocusEvent e){
			if(!label.equals(getText().trim())) {
				if(element instanceof ElementImport){// in the case of element import
					// do nothing
				} else if(element instanceof NamedElement) {
					Command command = GenericCommandFactory.eINSTANCE.createCommand(element, "Name", new Object[] {nameText.getText().trim()});
					// Darpan
					IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
					CommandStack stack = (CommandStack) editor.getAdapter(CommandStack.class);
					stack.execute(command);
					// ((NamedElement)element).setName(nameText.getText().trim());
				}
			}
		}
	};
}
